import os
import uuid
from datetime import datetime


# 生成并保存Markdown笔记
def save_markdown(result):
    # 构建web_notes文件夹路径
    web_notes_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "web_notes")
    # 确保web_notes目录存在
    os.makedirs(web_notes_dir, exist_ok=True)
    # 生成唯一的文件名
    file_uuid = uuid.uuid4().hex[:8]
    date = datetime.now().strftime('%Y-%m-%d')  # 2020-10-10/2025-05-01
    filename = f"note_{date}_{file_uuid}.md"
    # 构建完整的文件保存路径
    save_path = os.path.join(web_notes_dir, filename)
    # 保存到本地文件
    with open(save_path, "w", encoding="utf-8") as f:
        f.write(result)
    print(f"Markdown笔记已保存至：{save_path}")

    return save_path
